
import { ElMessage } from 'element-plus';
import router from '@/router'
import { useStore } from "vuex";
import axios from "axios";

import { Store } from 'vuex';
import store from "@/store/index";

const $store: Store<any> = store;
// const store = useStore();
// 清空路由历史记录的函数
function clearRouteHistory() {
    const getRoutes = router.getRoutes();
    getRoutes.forEach(route => {
        const { name } = route;
        if (name) {
            router.removeRoute(name);
        }
    });
}

const rejectFun = (err:any)=>{
    $store.commit("SET_LOGIN_STATE",false);
    router.push({path:'/login'});
    clearRouteHistory();
    ElMessage({
        type:"error",
        message:err.response.data.message
    });
    window.location.reload();
}

const http = (options:any)=>{
    return new Promise((resolve,reject)=>{
        if(options.type == 'post'){
            axios.post(options.url,options.params,options.headers).then(resolve).catch(err=>{
                const status = err.response.status;
                if(status == '401'){
                    rejectFun(err);
                    return;
                }
                reject();
            });
        }else{
            axios.get(options.url,options.params).then(resolve).catch(err=>{
                const status = err.response.status;
                if(status == '401'){
                    rejectFun(err);
                    return;
                }
                reject(err);
            });
        }
        
    })
}
//登录
export const login = (params:any)=>{
    return http({
        url:'/console/auth/v1/login/verify',
        type:"post",
        params:params
    });
    // return axios.post('/auth/v1/login/verify',params);
}
//修改密码
export const passwordModify = (params:any)=>{
    return http({
        url:'/console/user/v1/admin/update/password',
        type:"post",
        params:params
    });
    // return axios.post('/auth/v1/login/verify',params);
}
//退出登录
export const loginOut = ()=>{
    return http({
        url:'/console/auth/v1/login/out',
        type:"get"
    });
    // return axios.post('/auth/v1/login/verify',params);
}
//应用管理

//获取应用列表
export const getAppList = ()=>{
    return http({
        url:'/console/application/v1/controller/list',
        type:"get"
    });
    // return axios.get('/application/v1/controller/list')
}
//添加应用
export const addApp = (params:any)=>{
    return http({
        url:'/console/application/v1/controller/add',
        type:"post",
        params:params
    });
    // return axios.post('/application/v1/controller/add',params)
}

//获取当前应用下-版本列表
export const appVersionList = (params:any)=>{
    return http({
        url:'/console/appversion/v1/controller/list',
        type:"post",
        params:params
    });
    // return axios.post('/appversion/v1/controller/list',params);
}
//当前应用-上传新版本
export const uploadVersion = (params:any,headers:any)=>{
    return http({
        url:'/console/appversion/v1/controller/add',
        type:"post",
        params:params
    });
    // return axios.post('/appversion/v1/controller/add',params,headers);
}

//当前应用-启用/停用/删除当前版本
export const updateVersionStatus = (params:any)=>{
    return http({
        url:'/console/appversion/v1/controller/updateStatus',
        type:"post",
        params:params
    });
    // return axios.post('/appversion/v1/controller/updateStatus',params);
}


//下载连接

//获取下载连接列表
export const downloadLinkList = ()=>{
    return http({
        url:'/console/downloadlink/v1/controller/list',
        type:"get"
    });
    // return axios.get('/downloadlink/v1/controller/list');
}
//下载连接-添加连接
export const saveLink = (params:any)=>{
    return http({
        url:'/console/downloadlink/v1/controller/add',
        type:"post",
        params:params
    });
    // return axios.post('/downloadlink/v1/controller/add',params)
}
//下载连接-更新连接状态 启用/停用/删除 
export const updateLinkStatus = (params:any)=>{
    return http({
        url:'/console/downloadlink/v1/controller/updateStatus',
        type:"post",
        params:params
    });
    // return axios.post('/downloadlink/v1/controller/updateStatus',params)
}


//用户管理

//获取用户列表
export const getUserList = ()=>{
    return http({
        url:'/console/user/v1/admin/list',
        type:"post"
    });
    // return axios.post('/user/v1/admin/list');
}
//创建管理员
export const addUserData = (params:any)=>{
    return http({
        url:'/console/user/v1/admin/add',
        type:"post",
        params:params
    });
    // return axios.post('/user/v1/admin/add',params);
}
//生成访问令牌
export const getAccessToken = ()=>{
    return http({
        url:'/console/certificate/v1/manager/generate',
        type:"get"
    });
    // return axios.get('/certificate/v1/manager/generate');
}



